<?php
/**
 * @version     1.0.0
 * @package     com_student
 * @copyright   Copyright (C) 2011. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      duongca <duongca@ymail.com> - http://www.ohisee.co.cc
 */

defined('JPATH_BASE') or die;

jimport('joomla.html.html');
jimport('joomla.form.formfield');

/**
 * Supports an HTML select list of categories
 */
class JFormFieldClassField extends JFormField
{
	/**
	 * The form field type.
	 *
	 * @var		string
	 * @since	1.6
	 */
	protected $type = 'classfield';

	/**
	 * Method to get the field input markup.
	 *
	 * @return	string	The field input markup.
	 * @since	1.6
	 */
	 
	 
	protected function getInput()
	{
		$html = array();
		
		// Let's get the id for the current item, either category or content item.
		$jinput = JFactory::getApplication()->input;
		// Load the category options for a given extension.
		
		if ($jinput->get('option') == 'com_student')
		{
			$oldClass = $jinput->get('id', 0);
		}
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('a.id AS value, a.class_name AS text, a.state');
		$query->from('#__class AS a');
		
		$query->where('a.state = 1');

		$query->order('a.class_name ASC');
		
		$db->setQuery($query);
		
		
		$options = $db->loadObjectList();
		
		// Check for a database error.
		if ($db->getErrorNum()) {
			JError::raiseWarning(500, $db->getErrorMsg());
		}
		$html[] = '<select id="'.$this->id.'" name="'.$this->name.'">';
		
		$n = count($options);
		if($n == 0){
			$html[] = '<option value="" >Class is empty</option>';
		}else{
			$html[] = '<option value="" >Please select class</option>';
			for($i = 0; $i < $n; $i++)
			{
				if($oldClass == $options[$i]->value){
					$selected = 'selected="selected"';
				}else{
					$selected = '';
				}
				$html[] = '<option value="'.$options[$i]->value.'" '.$selected.' >'.$options[$i]->text.'</option>';
			}
		}
		$html[] = '</select>';

		return implode($html);		   
	}
}